package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117659217";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCDhl+3YHg7j1QFbHWP8vpdpfLqnpam6yssmJBrdeOs4nTfYbDuPJtSGt1/68ASKmCEwPt2XvC3lsFTiiL89Ugz7VYWQwXazDZpwt8OkhmjpimrHgTWRVxE2ofmpxanOohpXgiM5tqmslo6V3g6f7OEm6kOkVTRTVEvON++JbvxJbAOnxTmtO2GCqcEiH+CvesR7Jo83P6qrQXwZyPaAWIESBlaTYWEfkApQlKkmHGGXlbXOG5BfugglhX9aUHGKIpXRNcR0ZGwlDv61V/rCYUAE0PFgumGhwPY8viOIssuOBphOdUzVuc087RWHZSL7UeFZCHgmq3jh/sK/lF0bwHVAgMBAAECggEAAqEG/MVi4d3lkg4u82ep9YFBr8rnvAJrZILQMDW1DVmheG2decWgXS3IS6xMtJBASacJ373aFfFff+n89Q16Gf+efudsEImQdOC+UK4+8PR7VL5O3nGYfI/Zs5SXcVRMMb79cMOUj0x1XUEuWA0Ba58o6++fwGdJPiZm4ogij5ln1dS0jf3Sos7onI/swZLgnRSDbNK6Vx0YX7mrryy49wLnuaXrEYPoKRSRd/69Q4WGIc6TT1GqiWgSO7pSAuDwKSpKWPxgfB2DoqwZ9gToEKoq/1z6KFAW9t7hx4Sz6GgyPd7CEkjwLI40C+rjYwik4xAG+uGvDgnhZlaC0hpjwQKBgQDVnAi6GYGwsHMAWtZdBMFESJWrcMONgz49YZwZDcLT9Mrm+/PjsRD1zZnmUGWMZTPViz8s48JgmlzagvXZGBn6ka+FvfNrGgDw+qVQX2XZb3TMgI1Na2C54+/j23pm/so4AVYChL7fLLOLk57HMPyLcunynCqCf5VmXK06fyBzeQKBgQCdoDW98E1NIFTuSjnjqjkx7wfia4g2jx2WwsQfWN39gHeUwIVEIpOn93XlxRxfBUMyFkpU7cP7yPA3N+Zd5JbC8506zOLxuohl7qbAkoFCg0jwIWXf3C0TDUYtoiX2iIU1ODhBegYokiSuy01NLNCzh/D92Au8cjnSyCEuSeHuPQKBgQCLqS4nhv+9Z1B5ferD/JTkMVTyN5MT1ih+d3BZDR9B974l0bu/NgjxvVCPPid26MBAMX/HuM5g3OwyzgcxvslVg6UwG2aosuLErAwkBYuQ44eJIc/BO6CNSZOR9fEMoC/U+EKk+OfXAOIPMyBP+h5UinidcC93DTXALH3yTiYIeQKBgCx6cc/ICDgUQIMBJPauKPeDKKlmiIhEuCe7i9YAoJuQ7a1m6fQBAcNq27TbexcBCCYRDiaK9TuORuJ9/p5OC5C1Z4+ZEwz9AtIolptOe/UF0MYZ1oJlwb5Xh48G60l22ofa9lzbjvNR0dIVAhod5EJUwNcQaLdjUBe/a0WBiaRNAoGAYzMFzqmxunNN4i0zFW+qOK6egPK2eA+AWXnZrKih+TDKvMH+elQPyfk/fZgyz6Hi7848rU5s4vflwJzt2AFpF34MmDv50vORssy+q6LaLymtR+AUvmKmJijJtUAu3Nn5Bddx3NDfWmKRwyxcjfcYpp3isqPvObOSSUFvtBCiJ4I=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvyFi+UcC5EoJMEgrvAO6zv5Y1+viWT5uTj7PQbglIGevK7C5IVtduu1cYJUi1Z37mQ9fPTRpS9wUuGdMjyuTiSQvZRsJbRWVZRzzC+F9IFl/KF7lSRGSqw0Mzm9Zegmm2DORrKMCuIlxogkEi33CmrkV7/PNgD63TTbHtNx3HEc9aPHwCZolFIwk47hDnkZqR0zBHZJTDupgmx0IpsWH2xtuJdnKdUuMFOjMBSvfI3lw01v/ZJYHiXO4SkrR5iO7t5M7LbSw0Zuwmeg+b/jfSxYzEKxaX2/nRYWLkyILMyNEqO/EFjvzvDidWlbyw6YrQVYbDOxs4x7cTfTlJ8KIdwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://qovrghivc1.bjhttp.cn/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
